package banking;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import banking.gui.BankFrm;
import banking.gui.JDialog_AddCompAcc;
import banking.gui.JDialog_AddPAcc;
import framework.account.AccountManager;
import framework.account.Checking;
import framework.account.CompanyAccount;
import framework.account.PersonalAccount;
import framework.account.Saving;
import framework.customer.Address;
import framework.customer.Company;
import framework.customer.Person;
import framework.gui.JDialog_AddInterest;
import framework.gui.JDialog_Deposit;
import framework.gui.JDialog_GenReport;
import framework.gui.JDialog_Withdraw;
import framework.interfaces.IAccount;
import framework.interfaces.IAccountManager;
import framework.interfaces.IAccountType;
import framework.interfaces.IAddress;
import framework.interfaces.ICompany;
import framework.interfaces.IPerson;


public class BankAccountActionController {

	private static BankAccountActionController instance;

	private static IAccountManager accountMngr;

	private BankFrm view;

	private IAccount account;

	public static BankAccountActionController getInstance() {
		if (instance == null) {
			instance = new BankAccountActionController();
			accountMngr = new AccountManager();
		}
		return instance;
	}

	private class CreatePActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			IPerson customer;
			IAddress address;
			IAccountType accType;

			JDialog_AddPAcc pac = new JDialog_AddPAcc();
			pac.setVisible(true);

			String accNo = pac.getJTextField_ACNR().getText();
			String name = pac.getJTextField_NAME().getText();
			String street = pac.getJTextField_STR().getText();
			String city = pac.getJTextField_CT().getText();
			String state = pac.getJTextField_ST().getText();
			String zipCode = pac.getJTextField_ZIP().getText();
			String birthDate = pac.getJTextField_BD().getText();
			String eMail = pac.getJTextField_EM().getText();
			String type = "S";

			if (pac.getJRadioButton_Chk().isSelected()) {
				type = "Ch";
				accType = new Checking(accNo, 0, 0);
			} else {
				accType = new Saving(accNo, 0, 0);
			}

			if (pac.isOk()) {
				address = new Address(street, city, state, zipCode, eMail);
				customer = new Person(name, address, birthDate);
				account = new PersonalAccount(accType, customer);
				accountMngr.createAccount(account);

				Object[] rowdata = new Object[6];
				rowdata[0] = accNo;
				rowdata[1] = name;
				rowdata[2] = city;
				rowdata[3] = "P";
				rowdata[4] = type;
				rowdata[5] = "0";
				view.updateModel(rowdata);
				JOptionPane.showMessageDialog(null, "Personal account created.");
			}
		}
	}

	private class CreateCActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ICompany customer;
			IAddress address;
			IAccountType accType;

			JDialog_AddCompAcc pac = new JDialog_AddCompAcc();
			pac.setVisible(true);

			String accNo = pac.getJTextField_ACNR().getText();
			String name = pac.getJTextField_NAME().getText();
			String street = pac.getJTextField_STR().getText();
			String city = pac.getJTextField_CT().getText();
			String state = pac.getJTextField_ST().getText();
			String zipCode = pac.getJTextField_ZIP().getText();
			String numOfEmps = pac.getJTextField_NoOfEmp().getText();
			String eMail = pac.getJTextField_EM().getText();
			String type = "S";

			if (pac.getJRadioButton_Chk().isSelected()) {
				type = "Ch";
				accType = new Checking(accNo, 0, 0);
			} else {
				accType = new Saving(accNo, 0, 0);
			}
			
			if (pac.isOk()) {
				address = new Address(street, city, state, zipCode, eMail);
				customer = new Company(name, address, Integer.valueOf(numOfEmps));
				account = new CompanyAccount(accType, customer);
				accountMngr.createAccount(account);

				Object[] rowdata = new Object[6];
				rowdata[0] = accNo;
				rowdata[1] = name;
				rowdata[2] = city;
				rowdata[3] = "C";
				rowdata[4] = type;
				rowdata[5] = "0";
				view.updateModel(rowdata);
				JOptionPane.showMessageDialog(null, "Company account created.");
			}
		}
	}

	private class WithdrawActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			int selection = view.getJTable1().getSelectionModel().getMinSelectionIndex();
			if (selection >= 0) {
				String accNo = (String) view.getModel().getValueAt(selection, 0);

				JDialog_Withdraw wd = new JDialog_Withdraw(accNo);
				wd.setVisible(true);

				if (wd.isOk()) {
					if (((AccountManager) accountMngr).getAccountList().containsKey(accNo)) {
						double amount = Double.valueOf(wd.getJTextField_AMT().getText());
						accountMngr.withdraw(accNo, amount);
						amount = ((AccountManager) accountMngr).getAccountList().get(accNo).getAccountType().getBalance();
						view.updateModel(selection, amount);
						JOptionPane.showMessageDialog(null, "Transaction completed.");
					} else {
						JOptionPane.showMessageDialog(null, "Please check your account number.");
					}
				}
			}
		}
	}

	private class DepositActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			int selection = view.getJTable1().getSelectionModel().getMinSelectionIndex();
			if (selection >= 0) {
				String accNo = (String) view.getModel().getValueAt(selection, 0);

				JDialog_Deposit dep = new JDialog_Deposit(accNo);
				dep.setVisible(true);
				
				if (dep.isOk()) {
					double amount = Double.valueOf(dep.getJTextField_Deposit().getText());
					accountMngr.deposit(accNo, amount);
					amount = ((AccountManager) accountMngr).getAccountList().get(accNo).getAccountType().getBalance();
					view.updateModel(selection, amount);
					JOptionPane.showMessageDialog(null, "Transaction completed.");
				}
			}
		}
	}

	private class AddInterestActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			JDialog_AddInterest addInt = new JDialog_AddInterest();
			addInt.setVisible(true);
			if (addInt.isOk()) {
				String interest = addInt.getJTextField_Interest().getText();
				accountMngr.addInterest(Double.parseDouble(interest));
				JOptionPane.showMessageDialog(null, "Account interest updated.");
			}
		}
	}

	private class GenerateReporttActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String str = accountMngr.generateReport(null, null);
			JDialog_GenReport bill = new JDialog_GenReport(str);
			bill.setVisible(true);
		}
	}

	private class ExitActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}
	}

	public void setView(BankFrm view) {
		this.view = view;
	}

	public CreatePActionListener getCreatePActionListener() {
		return new CreatePActionListener();
	}

	public CreateCActionListener getCreateCActionListener() {
		return new CreateCActionListener();
	}

	public WithdrawActionListener getWithdrawActionListener() {
		return new WithdrawActionListener();
	}

	public DepositActionListener getDepositActionListener() {
		return new DepositActionListener();
	}

	public AddInterestActionListener getAddInterestActionListener() {
		return new AddInterestActionListener();
	}

	public GenerateReporttActionListener getGenerateReporttActionListener() {
		return new GenerateReporttActionListener();
	}
	
	public ExitActionListener getExitActionListener() {
		return new ExitActionListener();
	}
}
